package com.bodu.global.security;

import cn.hutool.core.util.ObjectUtil;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @author Administrator
 */
public class LoginUserUtil {

    /**
     * 活动登录用户信息
     *
     * @return
     */
    public static LoginUser getPrincipal() {
        try {
            return (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Long getUserId() {
        LoginUser loginUser = getPrincipal();
        if (ObjectUtil.isNotEmpty(loginUser)) {
            return loginUser.getId();
        }
        return null;
    }
}
